/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.domain;

import javax.persistence.*;
import java.util.Date;
import java.sql.Blob;

/**
 * Represents a page's resource.
 *
 * @author tmjee
 * @version $Date$ $Id$
 */
@Entity
@Table(name="TBL_RESOURCE")
@NamedQueries({
    @NamedQuery(
            name="Resource_findResourceById",
            query="SELECT resource FROM Resource AS resource WHERE resource.id = :resourceId"),
    @NamedQuery(
            name="Resource_findResourceByName",
            query="SELECT resource FROM Resource AS resource WHERE resource.name = :resourceName"),
    @NamedQuery(
            name="Resource_findResourceByNameAndId",
            query="SELECT resource FROM Resource AS resource WHERE resource.name = :resourceName AND resource.space.id = :spaceId")
})
@EntityListeners({EntityAuditListener.class})
public class Resource implements Identifiable,  PathAware  ,CreationInfoPersistable, ModificationInfoPersistable  {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Basic
    @Column(name="NAME")
    private String name;

    @Basic
    @Column(name="MIME_TYPE")
    private String mimeType;

    @Lob
    @Column(name="CONTENT")
    private byte[] content;
    
    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="MODIFIER_ID", referencedColumnName = "ID")
    private User modifier;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @Basic
    @Column(name="MODIFICATION_DATE")
    private Date modificationDate;

    @ManyToOne(targetEntity = Page.class)
    @JoinColumn(name="PAGE_ID", referencedColumnName = "ID")
    private Page page;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }


    public void setModifier(User modifier) {
        this.modifier = modifier;
    }

    public User getModifier() {
        return modifier;
    }


    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Resource resource = (Resource) o;

        if (name != null ? !name.equals(resource.name) : resource.name != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (mimeType != null ? mimeType.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (modificationDate != null ? modificationDate.hashCode() : 0);
        return result;
    }

    public String getFullPath() {
        return page.getFullPath() + "/" + name;
    }
}
